package com.fzg.common.demo.string;

import lombok.extern.slf4j.Slf4j;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * create by fzg
 * 2022/11/21 16:21
 * 字符串拼接的常用四种方法
 */

@Slf4j
public class StringSplicing {

    public static void main(String[] args) {
        // method1();
        // method2();
        // method3();
        method4();
    }

    // 字符串拼接：+
    public static void method1(){
        String a = "我是";
        String b = "fzg";
        String res = a + b;
        log.info(res);
    }

    // 字符串拼接：StringBuilder
    public static void method2(){
        String a = "我有";
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(a);
        int age = 23;
        stringBuilder.append(age).append("岁了。");
        log.info(stringBuilder.toString());
    }

    // 字符串拼接：StringBuffer
    public static void method3(){
        String a = "我是";
        StringBuilder s = new StringBuilder(a);
        s.append("男生");
        log.info(s.toString());
    }

    // 字符串拼接： string类的concat方法
    public static void method4(){
        String a = "今天是";
        String time = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(LocalDateTime.now());
        String weather = "  天气晴。";
        String res = a.concat(time).concat(weather);
        log.info(res);
    }

}
